package com.smsc.headend.task.engine.service.push.impl;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.smsc.headend.module.asset.entity.ComEntity;
import com.smsc.headend.module.asset.entity.Meter;
import com.smsc.headend.module.data.enums.OperationGroup;
import com.smsc.headend.module.data.enums.OperationName;
import com.smsc.headend.module.task.dto.AtomicTask;
import com.smsc.headend.module.task.dto.SubTaskGroup;
import com.smsc.headend.module.task.dto.Task;
import com.smsc.headend.module.task.enums.TaskType;
import com.smsc.headend.task.engine.service.FeignAssetManagementService;
import com.smsc.headend.task.engine.service.push.DcuRegisterTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @author wxh
 * @date 2021/6/3 14:01
 */
@Slf4j
@Service
public class DcuRegisterTaskServiceImpl implements DcuRegisterTaskService {
    @Autowired
    FeignAssetManagementService assetManagementService;

    @Override
    public Task dcuSetMeterListTask(Meter meter, Long dcuId) {
        ComEntity comEntity = assetManagementService.getComByDeviceId(dcuId);
        Task task = new Task();
        task.setIsODR(false);
        task.setMessageId(UUID.randomUUID().toString());
        task.setTaskNo(UUID.randomUUID().toString());
        task.setTaskType(TaskType.DcuPushUnRegisterMeter.toString());
        Map<String, Object> param = new HashMap<>();
        param.put("meterIds", Arrays.asList(meter.getMeterId()));
        task.setParamJson(JSONUtil.toJsonStr(param));
        task.setComId(comEntity.getComId());
        task.setPriority(Task.Priority.ABOVENORMAL.getPriorityCode());
        task.setRetryTimes(3);

        List<SubTaskGroup> subTaskGroups = new ArrayList<>();
        subTaskGroups.add(dcuSetMeterListSubTask(task));
        task.setSubTaskGroups(subTaskGroups);
        return task;
    }

    @Override
    public Task dcuDeleteMeterListTask(Meter meter, Long dcuId) {
        ComEntity comEntity = assetManagementService.getComByDeviceId(dcuId);
        Task task = new Task();
        task.setIsODR(false);
        task.setMessageId(UUID.randomUUID().toString());
        task.setTaskNo(UUID.randomUUID().toString());
        task.setTaskType(TaskType.DcuPushUnRegisterMeter.toString());
        Map<String, Object> param = new HashMap<>();
        param.put("meterIds", Arrays.asList(meter.getMeterId()));
        param.put("serialNums", Arrays.asList(meter.getMfgSn()));
        task.setParamJson(JSONUtil.toJsonStr(param));
        task.setComId(comEntity.getComId());
        task.setPriority(Task.Priority.ABOVENORMAL.getPriorityCode());
        task.setRetryTimes(3);

        List<SubTaskGroup> subTaskGroups = new ArrayList<>();
        subTaskGroups.add(dcuDeleteMeterListSubTask(task));
        task.setSubTaskGroups(subTaskGroups);
        return task;
    }

    private SubTaskGroup dcuSetMeterListSubTask(Task task) {
        SubTaskGroup subTaskGroup = new SubTaskGroup();
        subTaskGroup.setGroupId(OperationGroup.DCUSetMeterList.getId().longValue());
        AtomicTask atomicTask = new AtomicTask();
        atomicTask.setAtomicTaskNo(UUID.randomUUID().toString());
        atomicTask.setTaskNo(task.getTaskNo());
        atomicTask.setOperationId(OperationName.SetData.getId().longValue());
        atomicTask.setGroupId(OperationGroup.DCUSetMeterList.getId().longValue());
        atomicTask.setSeq(0);
        subTaskGroup.setSubTasks(Lists.newArrayList(atomicTask));

        return subTaskGroup;
    }

    private SubTaskGroup dcuDeleteMeterListSubTask(Task task) {
        SubTaskGroup subTaskGroup = new SubTaskGroup();
        subTaskGroup.setGroupId(OperationGroup.DCUDeleteMeterList.getId().longValue());
        AtomicTask atomicTask = new AtomicTask();
        atomicTask.setAtomicTaskNo(UUID.randomUUID().toString());
        atomicTask.setTaskNo(task.getTaskNo());
        atomicTask.setOperationId(OperationName.SetData.getId().longValue());
        atomicTask.setGroupId(OperationGroup.DCUDeleteMeterList.getId().longValue());
        atomicTask.setSeq(0);
        subTaskGroup.setSubTasks(Lists.newArrayList(atomicTask));

        return subTaskGroup;
    }
}

